package com.toman.ui.work.workshop;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Enum.BaseEnum;
import com.toman.Enum.PermEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.DevicehouseAdjustTableEntity;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DialogUtils;
import com.toman.Util.JsonUtils;
import com.toman.Util.PermissonUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.toolbar.ToolBarActivity;
import com.toman.widget.ClearEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
import okhttp3.Response;

public class CenterTableInfActivity extends ToolBarActivity {

    @BindView(R.id.tv_report_type)
    TextView tvReportType;
    @BindView(R.id.tv_beltline)
    TextView tvBeltline;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_product_mode)
    TextView tvProductMode;
    @BindView(R.id.tv_man)
    TextView tvMan;
    @BindView(R.id.et_remark)
    ClearEditText etRemark;
    @BindView(R.id.tv_create_man)
    TextView tvCreateMan;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.ll_create)
    LinearLayout llCreate;
    @BindView(R.id.tv_edit_man)
    TextView tvEditMan;
    @BindView(R.id.tv_edit_time)
    TextView tvEditTime;
    @BindView(R.id.ll_edit)
    LinearLayout llEdit;
    @BindView(R.id.tv_progress_man)
    TextView tvProgressMan;
    @BindView(R.id.tv_progress_time)
    TextView tvProgressTime;
    @BindView(R.id.ll_progress)
    LinearLayout llProgress;
    @BindView(R.id.tv_complete_man)
    TextView tvCompleteMan;
    @BindView(R.id.tv_complete_time)
    TextView tvCompleteTime;
    @BindView(R.id.ll_complete)
    LinearLayout llComplete;
    @BindView(R.id.tv_closecase_man)
    TextView tvClosecaseMan;
    @BindView(R.id.tv_closecase_time)
    TextView tvClosecaseTime;
    @BindView(R.id.ll_closecase)
    LinearLayout llClosecase;
    @BindView(R.id.tv_report_type_title)
    TextView tvReportTypeTitle;
    @BindView(R.id.tv_beltline_title)
    TextView tvBeltlineTitle;
    @BindView(R.id.tv_product_name_title)
    TextView tvProductNameTitle;
    @BindView(R.id.tv_product_mode_title)
    TextView tvProductModeTitle;
    @BindView(R.id.tv_man_title)
    TextView tvManTitle;
    @BindView(R.id.tv_report_num)
    TextView tvReportNum;

    private DevicehouseAdjustTableEntity bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_center_table_inf);
        ButterKnife.bind(this);
        initToolBar(BaseEnum.CENTERTABLE.name + "详情");
        initIntent();
        initView();
    }

    private void initView() {
        Object[] str = new Object[]{bean.getReportNo(), bean.getReportType(), bean.getBeltlineEntity().getBeltlineName(),
                bean.getProductEntity().getProductName(), bean.getProductEntity().getProductMode(), bean.getOperatorUsers().getRealName(), bean.getDescription()};
        ViewUtil.setText(str, tvReportNum,tvReportType, tvBeltline, tvProductName, tvProductMode, tvMan, etRemark);


        ViewUtil.unclick(tvReportType, tvBeltline, tvProductName, tvMan);
        ViewUtil.unedit(etRemark);
        tvCreateMan.setText(bean.getCreateUser().getRealName());
        tvCreateTime.setText(bean.getCreateTime());
        tvEditMan.setText(bean.getUpdateUser().getRealName());
        tvEditTime.setText(bean.getUpdateTime());
        tvCompleteMan.setText(bean.getCompleteUser().getRealName());
        tvCompleteTime.setText(bean.getCompleteTime());
        tvProgressMan.setText(bean.getHandleUser().getRealName());
        tvProgressTime.setText(bean.getHandleTime());
        tvClosecaseMan.setText(bean.getClosecaseUser().getRealName());
        tvClosecaseTime.setText(bean.getClosecaseTime());
        switch (status) {
            case "0":
                ViewUtil.gone(llClosecase, llComplete, llProgress);
                break;
            case "1":
                ViewUtil.gone(llClosecase, llComplete);

                break;
            case "2":
                ViewUtil.gone(llClosecase);
                break;

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_edit, menu);


        MenuItem menuItem = menu.findItem(R.id.menu_edit);
//        还没配置权限
        if (status.equals("1") || status.equals("2") || status.equals("3") || !PermissonUtils.contains(getApplicationContext(), BaseEnum.CENTERTABLE.has(PermEnum.EDIT))) {
            menuItem.setVisible(false);
        }
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_edit:
                        if (item.getTitle().equals("编辑")) {
                            item.setTitle("完成");
                            ViewUtil.click(tvReportType, tvBeltline, tvProductName, tvMan);
                            ViewUtil.edit(etRemark);
                            ViewUtil.necessary( tvReportTypeTitle, tvProductNameTitle, tvManTitle);

                        } else {
                            add();
                        }
                        break;
                }
                return false;
            }
        });

        return true;
    }

    private void initIntent() {
        status = getIntent().getStringExtra("status");
        bean = (DevicehouseAdjustTableEntity) getIntent().getSerializableExtra("bean");
    }

    @OnClick({R.id.tv_report_type, R.id.tv_beltline, R.id.tv_product_name, R.id.tv_man})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_report_type:
                DialogUtils.showDialog(this, "通知类型", getResources().getStringArray(R.array.ReportType), "", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setReportType(Integer.parseInt(bean1.getID()) + "");
                        tvReportType.setText(bean1.getName());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_beltline:

                DialogUtils.showDialog(this, "产线", NewBaseUrl.beltline_list_get, "beltlineName", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setBeltlineId(Long.parseLong(bean1.getID()));
                        tvBeltline.setText(bean1.getName());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_product_name:
                DialogUtils.showProductDialog(this, "品名", NewBaseUrl.workshop_product_list, "productName", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setProductId(Long.parseLong(bean1.getID()));
                        tvProductName.setText(bean1.getName());
                        tvProductMode.setText(bean1.getRemark());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_man:
                DialogUtils.showDialog(this, "操作工", NewBaseUrl.user_list_get, "realName", "userId", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setOperatorUser(Long.parseLong(bean1.getID()));
                        tvMan.setText(bean1.getName());
                        dialog.dismiss();
                    }
                });

                break;
        }
    }

    public void add() {
        String[] item = new String[]{"通知类型", "品名", "操作工"};//直接初始化
        if (ViewUtil.isNull(getApplicationContext(), item, tvReportType, tvProductName, tvMan)) {
            return;
        }


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("reportType", bean.getReportType());
        map.put("description", etRemark.getText().toString());
        map.put("beltlineId", bean.getBeltlineId());
        map.put("operatorUser", bean.getOperatorUser());
        map.put("productId", bean.getProductId());
        map.put("id", bean.getId());


        String str = JsonUtils.obj2JsonString(map);
        OkHttpUtils
                .post(NewBaseUrl.workshop_devicehouseadjusttable_update)
                .headers("token", token)
                .headers("Content-Type", "application/json")
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", getClass().toString() + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");
                        ViewUtil.lagout(code, getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });

    }

}
